package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.util.FileBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Part;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    @RequestMapping("/uploadImg")
    @ResponseBody
    public Object uploadImg(Part pic) throws Exception {
        FileBean fileBean = FileBean.newFileBean(pic);
        String path = fileBean.upload("localhost:8089");
        //String s = UploadUtil.uploadAli(pic);
        return path;
    }

    @RequestMapping("/uploadImg_ck")
    @ResponseBody
    public Map<String, Object> upload(Part upload, String module) {
        Map<String, Object> map = new HashMap<String, Object>();
        //String imagePath= null;
        if (upload != null && upload.getSize() > 0) {
            try {
                //图片保存, 返回路径
                //imagePath =  UploadUtil.uploadAli(upload);
                FileBean fileBean = FileBean.newFileBean(upload);
                String path = fileBean.upload("localhost:8089");
                //表示保存成功
                map.put("uploaded", 1);
                map.put("url", path);

            } catch (Exception e) {
                e.printStackTrace();
                map.put("uploaded", 0);
                Map<String, Object> mm = new HashMap<String, Object>();
                mm.put("message", e.getMessage());
                map.put("error", mm);
            }
        }
        return map;
    }

}
